//
//  ChangNickNameController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/27.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ChangNickNameController.h"

@interface ChangNickNameController ()
/**<#属性#>*/
@property (nonatomic,strong)UIView*bgview;
/**<#属性#>*/
@property (nonatomic,strong)PubulicTextView*textView;
/**<#属性#>*/
@property (nonatomic,strong)UILabel*numLabel;

@end

@implementation ChangNickNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.bgview];
    
    [self.bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    UIView * view = UIView.new;
    view.backgroundColor = KWhiteColor;
    
    UILabel * label = UILabel.new;
    label.text = @"支持中英文 数字";
    label.textColor = UIColorFromRGB(0x959E97);
    label.font = SYSTEMFONT(12);
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.titleLabel.font = SYSTEMFONT(16);
    ViewRadius(button, 5);
    [button setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
    
    kWeakSelf(weakSelf);
    [button addActionHandler:^{
        DLog(@"%@",weakSelf.textView.textView.text);
        NSString * text = weakSelf.textView.textView.text;
        if (text.length == 0) {
            ShowHubMessage(@"用户名不能为空");
            return ;
        }
        BlockSafeRun(weakSelf.tapBlock,text);
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.bgview addSubview:view];
    [self.bgview addSubview:self.textView];
    [self.bgview addSubview:button];
    [self.bgview addSubview:label];
    [self.bgview addSubview:self.numLabel];

    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-60);
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(view).offset(-8);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view);
        make.top.mas_equalTo(view.mas_bottom).offset(8);
    }];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.mas_equalTo(-16);
        make.width.mas_equalTo(56);
        make.height.mas_equalTo(26);
    }];
}

-(UILabel*)numLabel{
    if(!_numLabel){
        _numLabel = UILabel.new;
        _numLabel.text = @"20";
        _numLabel.textColor = UIColorFromRGB(0x959E97);
        _numLabel.font = SYSTEMFONT(14);
    }
    return _numLabel;
}
-(PubulicTextView*)textView{
    if(!_textView){
        _textView=[[PubulicTextView alloc] initWithFrame:CGRectZero];
        _textView.backgroundColor = UIColor.clearColor;
        _textView.textView.backgroundColor =UIColor.clearColor;
        _textView.textView.font = SYSTEMFONT(16);
        _textView.placeholderText.text = @"请输入用户名";
        _textView.maxTextCount = 20;
        kWeakSelf(weakSelf);
        _textView.backIntegerBlock = ^(NSInteger index) {
            if (index>=20) {
                index = 20;
            }
            weakSelf.numLabel.text = [NSString stringWithFormat:@"%ld",20-index];
        };
    }
    return _textView;
}
-(UIView*)bgview{
    if(!_bgview){
        _bgview= [[UIView alloc] initWithFrame:CGRectZero];
        _bgview.backgroundColor = UIColorFromRGB(0xF5F7F5);
    }
    return _bgview;
}
-(void)setDefutName:(NSString *)defutName{
    _defutName = defutName;
    self.textView.textView.text = defutName;
    self.textView.placeholderText.hidden = YES;
}
@end
